######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate an application pool
# -----------------------------------------------------------------
function New-ApplicationPool()
{
    $obj=new-object PSObject -Property @{
        Name = "";
        Account = "";
    }
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "    Pool"  
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Account" $this.Account
    }
	# -----------------------------------------------------------------
    # Registers the account as a managed Account
    # -----------------------------------------------------------------
	Add-Method $obj "RegisterAccount" `
    {
		return Register-ManagedAccount($this.Account)
    }
    # -----------------------------------------------------------------
    # Provisions the Application Pool
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $pool = Get-SPServiceApplicationPool  | Where-Object {$_.Name -eq $this.Name}
        if($pool -eq $null)  
        {  
            Write-Operation "Creating Application Pool $($this.Name) using $($this.User)" 

            $ma = $this.RegisterAccount()
            $pool = New-SPServiceApplicationPool -Name $this.Name -Account $this.Account   
        }
         
        return $pool
    }
    return $obj
}
